package com.asia.algorithmcode.dynamicProgram;

/**
 * @DESCRIPTION: 121、买卖股票的最佳时机
 * @USER: wanfu
 * @DATE: 2025/6/5 星期四 14:10
 */
public class MaxProfit {


    public static void main(String[] args) {
        int[] arr = {7, 1, 5, 3, 6, 4};
        System.out.println(new MaxProfit().maxProfit(arr));
    }


    public int maxProfit(int[] prices) {
        int n = prices.length;
        int minPrice = Integer.MAX_VALUE;
        int maxValue = 0;

        for (int i = 0; i < n; i++) {
            if (prices[i] < minPrice) {
                minPrice = prices[i];
            } else if (prices[i] - minPrice > maxValue) {
                maxValue = prices[i] - minPrice;
            }
        }
        return maxValue;
    }


    /**
     * 暴力解法，超时
     * @param prices
     * @return
     */
//    public int maxProfit(int[] prices) {
//        int n = prices.length;
//        int max = 0;
//        for (int i = 0; i < n; i++) {
//            for (int j = i + 1; j < n; j++) {
//                if (prices[j] > prices[i] && prices[j] - prices[i] > max) {
//                    max = prices[j] - prices[i];
//                }
//            }
//        }
//        return max;
//    }

}
